﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VgoCardChange.Models.Objects;
using VgoCardChange.Models.DAL;

namespace VgoCardChange.Models
{
    public class RoleModels
    {
        public List<Role> GetRoles()
        {
            List<Role> listrole = new List<Role>();
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "select RoleID,RoleName,Desciption from TblRole";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Role role = new Role();
                    role.RoleID = int.Parse(con.dataRead["RoleID"].ToString());
                    role.RoleName = con.dataRead["RoleName"].ToString();
                    role.Desciption = con.dataRead["Desciption"].ToString();
                    listrole.Add(role);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
            return listrole;
        }
        public void CreateRole(Role role)
        {
            ConnectDB con=new ConnectDB();
            try
            {
                string sql = "sp_CreateRole @RoleName=N'" + role.RoleName + "', @Desciption=N'" + role.Desciption + "' ";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally
            { con.CloseData(); con = null; }
        }
        public void EditRole(Role role)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_EditRole @RoleID ='"+role.RoleID+"',@RoleName=N'"+role.RoleName+"', @Desciption=N'"+role.Desciption+"'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally
            { con.CloseData(); con = null; }
        }
        public void DeleteRole(int RoleID)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_DeleteRole @RoleID='"+RoleID+"'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally
            { con.CloseData(); con = null; }
        }
        public List<Role> GetRoleID(int RoleID)
        {

            List<Role> listroleid = new List<Role>();
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_GetRoleID @RoleID='"+RoleID+"'";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Role role = new Role();
                    role.RoleID = int.Parse(con.dataRead["RoleID"].ToString());
                    role.RoleName = con.dataRead["RoleName"].ToString();
                    role.Desciption = con.dataRead["Desciption"].ToString();
                    listroleid.Add(role);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
            return listroleid;
        }
    }
}